package cn.broadsense.thrid.api;

import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.*;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 事件编排 相关接口
 *
 * @author pengxin
 * @date 2022-10-09 14:22
 */
@Slf4j
@Component
public class EventRepository extends BaseRepository {

    /**
     * 添加任务编排
     *
     * @param param
     * @return
     */
    public EvPlanTaskResult addPlanTask(EvPlanTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK);
        return DaHuaHttpUtils.postRest(token, param);
    }


    /**
     * 更新任务编排
     *
     * @param param
     * @return
     */
    public EvPlanTaskResult updatePlanTask(String planTaskCode,EvUpPlantTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.UPDATE_PLAN_TASK+planTaskCode);
        return DaHuaHttpUtils.putRest(token, param);
    }

    /**
     * 启停任务
     *
     * @param planTaskId
     * @param enableStatus 1启动，2未启动
     * @return
     */
    public JSONObject changePlanTask(String planTaskId, Integer enableStatus) {
        String token = getToken();
        EvChangeTaskParam param = new EvChangeTaskParam();
        param.setEnableStatus(enableStatus);
        param.setUrl(url + String.format(DaHuaApiUrlConstant.EventAnalysis.CHANGE_PLAN_TASK_STATUS, planTaskId));
        return  DaHuaHttpUtils.putRest(token, param);
    }

    /**
     * 删除任务
     *
     * @param planTaskId
     */
    public void delPlanTask(String planTaskId) {
        String token = getToken();
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return String.class;
            }
        };
        param.setUrl(url + String.format(DaHuaApiUrlConstant.EventAnalysis.DEL_PLAN_TASK, planTaskId));
        DaHuaHttpUtils.deleteRest(token, param);
    }


    /**
     * 分页查询任务
     *
     * @param param
     * @return
     */
    public EvPlanTaskPageResult planTaskPage(EvPlanTaskPageParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK_LIST);
        return DaHuaHttpUtils.getRest(token, param);
    }

    /**
     * 查询任务详情
     *
     * @param planTaskId
     * @return
     */
    public EvPlanTaskDetails planTaskDetails(String planTaskId) {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return EvPlanTaskDetails.class;
            }
        };
        param.setUrl(url + String.format(DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK_DETAILS, planTaskId));
        return DaHuaHttpUtils.getRest(token, param);
    }


    /**
     * 查询任务能力占用接口
     *
     * @param param
     * @return
     */
    public EvTaskCapacityUsageResult taskCapacityUsage(EvTaskCapacityUsageParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK_CAPACITYUSAGE);
        return DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 新增通道任务
     *
     * @param param
     * @return
     */
    public EvChannelTaskResult addChannelTask(EvChannelTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.CHANNEL_TASK);
        return DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 删除通道任务
     *
     * @param param
     * @return
     */
    public EvChannelTaskResult delChannelTask(EvChannelTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.CHANNEL_TASK);
        return DaHuaHttpUtils.deleteRest(token, param);
    }


    /**
     * 更新通道任务
     *
     * @param param
     * @return
     */
    public EvChannelTaskResult updateChannelTask(EvChannelTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.CHANNEL_TASK);
        return DaHuaHttpUtils.putRest(token, param);
    }

    /**
     * 算法重启
     *
     * @param param
     */
    public void restartAlgorithm(EvAlgorithmRestartParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.EVENT_ALGORITHM_RESTART);
        DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 批量启停任务
     *
     * @param param
     */
    public String batchChangeTask(EvBatchChangeTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.CHANGE_BATCH_PLAN_TASK_STATUS);
        JSONObject result = DaHuaHttpUtils.putRest(token, param);
        return result.getString("enableStatus");
    }

    /**
     * 批量删除
     *
     * @param param
     */
    public void batchDelTask(EvBatchDelTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.DELETE_BATCH_PLAN_TASK);
        DaHuaHttpUtils.deleteRest(token, param);
    }

    /**
     * 运行任务实时统计
     *
     * @return
     */
    public EvTaskStatisticRunResult runningTasks() {
        String token = getToken();
        EvTaskStatisticRunParam param = new EvTaskStatisticRunParam();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.STATISTIC_RUNNING_TASKS);
        return DaHuaHttpUtils.getRest(token, param);
    }

    /**
     * 运行任务实时统计
     *
     * @return
     */
    public EvTaskStatisticAnalysisResult analysisTasks() {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return EvTaskStatisticAnalysisResult.class;
            }
        };
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.STATISTIC_ANALYSIS_TASKS);
        return DaHuaHttpUtils.getRest(token, param);
    }


    /**
     * 运行任务用户实时统计
     *
     * @return
     */
    public EvUserTasksResult userTasks() {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return EvUserTasksResult.class;
            }
        };
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.STATISTIC_RUNNING_USER_TASKS);
        return DaHuaHttpUtils.getRest(token, param);
    }


    /**
     * 点位任务查询
     *
     * @param param
     * @return
     */
    public EvQueryChannelTaskResult planTaskChannel(EvQueryChannelTaskParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK_CHANNEL);
        return DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 全量同步编排任务状态信息
     *
     * @param param
     * @return
     */
    public EvDualNetWorkResult dualNetWork(EvDualNetWorkParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.PLAN_TASK_SYNC_STATUS);
        return DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 已加入分析的点位列表
     *
     * @param param
     * @return
     */
    public BasePageResult<FunctionChannelListResult> functionChannelList(FunctionChannelListParam param){
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.FUNCTION_CHANNEL_LIST);
        BasePageResult<FunctionChannelListResult> result = DaHuaHttpUtils.postRest(token, param);
        if(result!=null && CollUtil.isNotEmpty(result.getResults())){
            String s = JSON.toJSONString(result.getResults());
            List<FunctionChannelListResult> listResultList = JSON.parseArray(s, FunctionChannelListResult.class);
            result.setResults(listResultList);
        }
        return result;
    }

    public ViasSystemStatusResult  systemStatus(){
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }
            @Override
            public Class getResultClass() {
                return ViasSystemStatusResult.class;
            }
        };
        param.setUrl(url + DaHuaApiUrlConstant.EventAnalysis.VIAS_SYSTEM_STATUS);
        return DaHuaHttpUtils.getRest(token, param);
    }
}


